package com.example.yuxin.demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.example.yuxin.demo.service.UserService;
import com.example.yuxin.demo.vo.User;

/**
 * Description: <br>
 *
 * @author: name：yuxin <br>email: yuruixin@ixincheng.com <br>
 * Create Time:  2018/7/8 0008-下午 1:58<br>
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(String name, Integer age) {
        jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);
    }

    @Override
    public void deleteByName(String name) {
        jdbcTemplate.update("delete from USER where NAME = ?", name);
    }

    @Override
    public Integer getAllUsers() {
        return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);
    }

    @Override
    public List<User> getAllUserList() {
        return jdbcTemplate.query("select * from USER", new BeanPropertyRowMapper<>(User.class));
    }

    @Override
    public User getUserById(Long id) {
        return jdbcTemplate.queryForObject("select * from USER where id = ?", new BeanPropertyRowMapper<User>(User.class),id);
    }

    @Override
    public void updateUserById(Long id, String name, Integer age) {
        jdbcTemplate.update("update USER set NAME = ?,age = ? where id=?", name,age,id);
    }

    @Override
    public void deleteAllUsers() {
        jdbcTemplate.update("delete from USER");
    }

    @Override
    public void deleteById(Long id) {
        jdbcTemplate.update("delete from USER where id = ?",id);
    }
}
